package com.handle.system.web.controller;

import com.handle.common.core.dto.token.User;
import com.handle.common.core.valid.AddGroup;
import com.handle.common.core.valid.UpdateGroup;
import com.handle.system.api.constants.ResponseEnum;
import com.handle.common.core.result.JsonResult;
import com.handle.common.core.utils.ContextHolderUtils;
import com.handle.system.api.dto.SysMenuDto;
import com.handle.system.api.entity.SysMenu;
import com.handle.system.api.request.menu.MenuAddOrEditParam;
import com.handle.system.api.request.menu.MenuQueryParam;
import com.handle.system.api.request.role.RolePermissionParam;
import com.handle.system.api.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author zhangqing
 * @description 菜单控制层
 * @date 2021/4/26 8:00 下午
 */
@RestController
@RequestMapping("/menu")
@AllArgsConstructor
@Api(tags = "SysMenuController", value = "菜单控制层")
public class SysMenuController {
    private final ISysMenuService sysMenuService;

    @ApiOperation("获取当前登录人菜单")
    @GetMapping("/info")
    public JsonResult menuInfo(){
        User userDto = ContextHolderUtils.getCurrentUser();
        ResponseEnum.UNAUTHORIZED.assertIsEmpty(userDto);
        List<SysMenu> sysMenuDtos = sysMenuService.selectMenusByUser(userDto);
        return JsonResult.success(sysMenuDtos);
    }

    @ApiOperation("[Remote->Feign] 根据用户ID查询权限")
    @GetMapping("perms/{userId}")
    public Set<String> perms(@PathVariable("userId") Long userId) {
        return sysMenuService.selectPermsByUserId(userId);
    }

    @ApiOperation("查询角色所有的菜单权限(用于勾选)")
    @GetMapping("/findMenuByRoleId/{roleId}")
    public JsonResult findMenuByRoleId(@PathVariable("roleId") Long roleId) {
        return JsonResult.success(sysMenuService.findMenuByRoleId(roleId));
    }

    @ApiOperation("查询所有的菜单(树形结构)")
    @GetMapping("/findAllMenuTree")
    public JsonResult findAllMenuTree() {
        return JsonResult.success(sysMenuService.findAllMenu());
    }

    @ApiOperation("查询所有的菜单(列表结构包含按钮权限)")
    @GetMapping("/findMenuAndPermissionList")
    public JsonResult findMenuAndPermissionList() {
        return JsonResult.success(sysMenuService.findMenuAndPermissionList());
    }

    @ApiOperation("查询所有的菜单(列表结构包含按钮权限)")
    @PostMapping("/findMenuList")
    public JsonResult findMenuList(@RequestBody MenuQueryParam menuQueryParam) {
        return JsonResult.success(sysMenuService.findMenuList(menuQueryParam));
    }

    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public JsonResult addMenu(@Validated({AddGroup.class}) @RequestBody MenuAddOrEditParam menuAddOrEditParam){
        return JsonResult.toJsonResult(sysMenuService.addMenu(menuAddOrEditParam));
    }

    @ApiOperation("修改菜单")
    @PutMapping("/editMenu")
    public JsonResult editMenu(@Validated({UpdateGroup.class}) @RequestBody MenuAddOrEditParam menuAddOrEditParam){
        return sysMenuService.editMenu(menuAddOrEditParam);
    }

    @ApiOperation("检查菜单名称是否唯一")
    @GetMapping("/checkMenuName")
    public JsonResult checkMenuName(String menuName,String menuId){
        return JsonResult.success(sysMenuService.checkMenuName(menuName,menuId));
    }


    @ApiOperation("检查菜单唯一键是否唯一")
    @GetMapping("/checkMenuKey")
    public JsonResult checkMenuKey(String menuKey,String menuId){
        return JsonResult.success(sysMenuService.checkMenuKey(menuKey,menuId));
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/delMenu/{menuId}")
    public JsonResult delMenu(@PathVariable("menuId") Long menuId){
        return JsonResult.toJsonResult(sysMenuService.delMenu(menuId));
    }

    @ApiOperation("获取菜单详情")
    @GetMapping("/findMenuDetails/{menuId}")
    public JsonResult findMenuDetails(@PathVariable("menuId") Long menuId){
        return sysMenuService.findMenuDetails(menuId);
    }
}
